﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.Menu;
using B.S.BasicData.Api.Read.DTO;
using B.S.BasicData.Domain;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using Microsoft.EntityFrameworkCore.Query.Internal;

namespace B.S.BasicData.Api.Read.Application.Handler.Menu
{
    public class GetParentMenuHandler : IRequestHandler<GetParentMenuCommand, ApiResult<List<MenuDTO>>>
    {
        private readonly IBaseRepository<MenuModel> menuRepo;
        private readonly IMapper mapper;

        public GetParentMenuHandler(IBaseRepository<MenuModel> menuRepo,IMapper mapper)
        {
            this.menuRepo = menuRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<MenuDTO>>> Handle(GetParentMenuCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<MenuDTO>> res = new ApiResult<List<MenuDTO>>();
            var menu = menuRepo.GetAll().Where(x=>  x.IsDelete == false).ToList();
            var menudtos = menu.Select(menu => new MenuDTO
            {
                MenuName = menu.MenuName,
                CreateName = menu.CreateName,
                CreateTime = menu.CreateTime,
                Id = menu.Id,
                ParentName = menu.MenuName,
                Pid = menu.Pid,
                UpdateName = menu.UpdateName,
                UpdateTime = menu.UpdateTime,
                Url = menu.Url,

            }).ToList();
            res.Code = ApiEnum.Success;
            res.Data = menudtos;
            res.Msg = "查询成功";
            return Task.FromResult(res);    
        }
    }
}
